package edu.umassd.hciproject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.EditText;
import edu.umassd.hciproject.SaveDialogFragment.SaveDialogListener;


public class EditPlaceDialogFragment extends DialogFragment {

	OnSaveListener mListener;
	EditText nameBox = null, addressBox = null;
	String name = null, address = null;
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				mListener.onClick(EditPlaceDialogFragment.this, nameBox.getText().toString(), addressBox.getText().toString());
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});
		View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_edit_place, null);
		nameBox = (EditText) view.findViewById(R.id.etPlaceName);
		if (name != null)
			nameBox.setText(name);
		addressBox = (EditText) view.findViewById(R.id.etPlaceAddress);
		if (address != null)
			addressBox.setText(address);
		builder.setView(view);
		builder.setTitle("Edit Place");
		AlertDialog dialog = builder.create();
		return dialog;
	}
	
	public void setPlaceName(String name) {
		this.name = name; 
		if (nameBox != null)
			nameBox.setText(name);
	}
	
	public void setPlaceAddress(String address) {
		this.address = address;
		if (addressBox != null)
			addressBox.setText(address);
	}
	
	public interface OnSaveListener{
		public void onClick(DialogFragment dialog, String name, String address);
	}
	
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (OnSaveListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement EditPlaceDialogFragment.OnSaveListener");
        }
    }
	
}
